﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using SqlLibrary;

public partial class Ctl_ctlCalendar : System.Web.UI.UserControl
{
    public Func<bool>  OnPeriodChange;


    protected void Page_Load(object sender, EventArgs e)
    {
        InitTaskListEditor();
    }


        public int lvEditIndex 
    {
        get { return lvCalendar.EditIndex; }
        set {  lvCalendar.EditIndex = value; }
    }

    public void  ResetEditIndex()
    {
        lvCalendar.EditIndex = -1; 
    }


    public TaskCalendar.CalendarPeriod PeriodType
    {
        get
        {
            if (hfTerm.Value.Equals(TaskCalendar.CalendarPeriod.Month.ToString())) return TaskCalendar.CalendarPeriod.Month;
            if (hfTerm.Value.Equals(TaskCalendar.CalendarPeriod.Week.ToString())) return TaskCalendar.CalendarPeriod.Week;
            return TaskCalendar.CalendarPeriod.Day;


        }
        set
        {
            if (value.Equals(TaskCalendar.CalendarPeriod.Month)) hfTerm.Value = TaskCalendar.CalendarPeriod.Month.ToString();
            if (value.Equals(TaskCalendar.CalendarPeriod.Week)) hfTerm.Value = TaskCalendar.CalendarPeriod.Week.ToString();
            if (value.Equals(TaskCalendar.CalendarPeriod.Day))hfTerm.Value = TaskCalendar.CalendarPeriod.Day.ToString();

        }
    }

    public DateTime AnchorDay 
    { 
        get{
            if (hfAnchorDay.Value == string.Empty) 
                hfAnchorDay.Value = DateTime.Now.ToString (); 
        
            return Convert.ToDateTime(hfAnchorDay.Value); 
            } 
        
        set { hfAnchorDay.Value   = value.ToShortDateString (); } 
    }

    public string  EmplID
    {
        get
        {
            
            return hfEmplID .Value ;
        }

        set { hfEmplID.Value  = value; }
    }

    public string ClientID
    {
        get
        {

            return hfClientID.Value;
        }

        set { hfClientID.Value = value; }
    }

    
    public ListWhere  Where
    {
        set { _Calendar.Where = value ; }
        get { return _Calendar.Where; }
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        MyDataBind();
        
    }

    private void MyDataBind()
    {
        _Calendar.Where = Where;                
        lvCalendar.DataSource = _Calendar.Items;
        lvCalendar.DataBind();
        InitTaskListEditor();
        if (OnPeriodChange != null) OnPeriodChange();
    }

    private TaskCalendar _Calendar
    {
        get { return TaskCalendar.GetCalendar (AnchorDay, PeriodType ); }        
    }

    private void InitTaskListEditor()
    {
        if (null!=lvCalendar.EditItem )
        {
            Ctl_ctlTaskEditor t = (Ctl_ctlTaskEditor)lvCalendar.EditItem.FindControl("ctlTaskEditor1");
            t.OnMyDataChanged = OnTaskEditorDataChanged;
            t.OnCancelClick = OnTaskEditorClick;
        }
    }

    protected void ReverceTaskState(object sender, EventArgs e)
    {
        string s = ((Button)sender).CommandArgument;
        wsTask ws = (wsTask)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Task];
        Task t = ws.GetTask(s);
        t.IsClosed = !t.IsClosed;
        ws.SaveTask(t);
        OnDataBinding(null);

    }


    

    protected void RemoveTask_Click(object sender, ImageClickEventArgs e)
    {   
        wsTask ws = (wsTask)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Task];
        ImageButton btn = (ImageButton)sender;
        Task t = ws.GetTask(btn.CommandArgument);
        t.DestroyMySelf();
        ws.SaveTask(t);
        OnDataBinding(null);
    }

    protected void lvCalendar_ItemEditing(object sender, ListViewEditEventArgs e)
    {   
        
    }
   
    protected void lvCalendar_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

    }
    
    protected void lvCalendar_ItemCommand(object sender, ListViewCommandEventArgs e)
    {

        ListView lv = (ListView)sender;
        switch (e.CommandName)
        {
            case "Edit":
                {
                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = d.DataItemIndex;
                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    lv.EditIndex = -1;
                    break;
                }
            case "Delete":
                {
                    //DeleteCustomer(e.CommandArgument as string);
                    break;
                }
            case "Cancel":
                {
                    lv.EditIndex = -1;
                    break;
                }
         }
            OnDataBinding(null);
        
    }

    public  string PeriodAsString
    {
        get
        {
            return _Calendar.GetPeriodAsText;
        }
    }

    protected void btnPrev_Click(object sender, EventArgs e)
    {
        AnchorDay = _Calendar.MinusTerm();
        lvCalendar.EditIndex = -1;
        DataBind();
    }
    
    public void MinusTerm()
    {
        AnchorDay = _Calendar.MinusTerm();
        lvCalendar.EditIndex = -1;
        DataBind();
    }

    public void PlusTerm()
    {
        AnchorDay = _Calendar.AddTerm();
        lvCalendar.EditIndex = -1;
        DataBind();
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        AnchorDay = _Calendar.AddTerm();
        lvCalendar.EditIndex = -1;
        DataBind();
        
    }

    protected void btnAddTask_Click(object sender, EventArgs e)
    {        
        wsTask WS = (wsTask)GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Task];
        Button btn = (Button)sender;
        DateTime dt = Convert.ToDateTime(btn.CommandArgument);
        Task t = new Task() { dtFinish = dt, TaskContent ="Новая задача", EmplID=EmplID , ClientID =ClientID };
        t.ID = Guid.NewGuid().ToString() + GlobalConstants.qPrefNEW_RECORD;
        WS.SaveTask(t);
        OnDataBinding(null);
    }

    public DateTime dtStrat { get { return _Calendar._Day; } }
    
    public DateTime dtFinish { get { return _Calendar._LastDay; } }


    protected bool OnTaskEditorClick()
    {
        lvCalendar.EditIndex = -1;
        MyDataBind();
        return true;
    }
   
    protected bool OnTaskEditorDataChanged()
    {        
        return true;
    }
    
    protected void lvCalendar_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
    //    ListViewDataItem r = (ListViewDataItem)e.Item;
    //    grdCalendarRow row = (grdCalendarRow)r.DataItem;        
    }

    string CurrentBoudTerm = string.Empty;
    protected string TermText(string s)
    {


        if (!CurrentBoudTerm.Equals(s))
        {
            CurrentBoudTerm = s; ;
            return s;
        }
        return string.Empty;          
    }

    protected bool GetGroupToggleIsVisible(string s)
    {   
            if (PeriodType == TaskCalendar.CalendarPeriod.Day) return false;
            return !CurrentBoudTerm.Equals(s);        
    }

    protected void btnDaySet_Click(object sender, EventArgs e)
    {
        SetPeriod(TaskCalendar.CalendarPeriod.Day);        
    }

    public void SetPeriod(TaskCalendar.CalendarPeriod Period)
    {
        AnchorDay = DateTime.Now;
        PeriodType = Period ;
        DataBind();
    }

    protected void btnWeekset_Click(object sender, EventArgs e)
    {
        SetPeriod(TaskCalendar.CalendarPeriod.Week);
    }


    protected bool IsMyCaLendarMode(string sMode)    
    {
        return (sMode == PeriodType.ToString());  
        
    }
    
    protected void ImageButton3_Click(object sender, CommandEventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        DateTime t = Convert.ToDateTime(btn.CommandArgument);
        if (PeriodType == TaskCalendar.CalendarPeriod.Day) ;
        {
            AnchorDay = t;
            PeriodType = TaskCalendar.CalendarPeriod.Day;
            DataBind();
        }

    }
}
